#pragma once

#include <iostream>
#include <string>

#define DIE(code)   \
    do              \
    {               \
        exit(code); \
    } while (0)

#define CONV(sock) (struct sockaddr *)sock

enum
{
    USAGE_ERR = 1,
    SOCKET_ERR,
    BIND_ERR,
    SENDTO_ERR,
    LISTEN_ERR,
    CONNECT_ERR,
    SEND_ERR,
    ACCEPT_ERR
};

bool ParseOneLine(std::string &req_str, std::string *req_line, const std::string sep)
{
    auto pos = req_str.find(sep);
    if (pos == std::string::npos)
        return false;

    // 讲获取到的第一行信息带出
    *req_line = req_str.substr(0, pos);
    // 删掉获取掉的第一行信息
    req_str.erase(0, pos + sep.size());
    return true;
}

// 获取 key  val
bool SplitRule(std::string &str, const std::string &flag, std::string *key, std::string *val)
{
    auto pos = str.find(flag);
    if (pos == std::string::npos) // 没找到 flag
        return false;
    *key = str.substr(0, pos);
    *val = str.substr(pos + flag.size());
    return true;
}
